//生成会员表云函数
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const _openid = wxContext.OPENID

  const { data  } = await db.collection('user').where({ _id:_openid }).get()
  if(data.length == 0){
    let { _id } = await db.collection('user').add({
      data: {
        _id: _openid,
        totalPoints: 0,
        tnum: 0,
        ...event.userInfo,
      },
      success: res => {
        wx.showToast({ title: '新增记录成功', })
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '新增记录失败'
        })
      }
    })
  }
}